<template>
  <div class="logs-container">
    <div class="logs-box">
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        :inline="false"
        size="normal"
      >
        <div class="title" @click="goHome">博客首页</div>
        <el-form-item>
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="loginForm.password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="status" type="primary" @click="handleLogin"
            >登 录</el-button
          >
          <el-button v-else type="primary" @click="handleRegister"
            >注 册</el-button
          >
        </el-form-item>
        <div class="login-register">
          <p :class="status ? 'active' : ''" @click="clickLogin">登录</p>
          <p :class="status ? '' : 'active'" @click="clickRegister">注册</p>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import logsApi from "@/api/logs";
export default {
  name: "Logs",
  data() {
    return {
      status: true,
      loginForm: {
        username: "",
        password: "",
      },
      loginFormRules: {},
    };
  },
  created() {
    console.log(this.$store.getters.getUser);
  },
  methods: {
    handleLogin() {
      logsApi
        .login(this.loginForm)
        .then((res) => {
          const jwt = res.headers["authorization"];
          if (res.data.code === 200) {
            const userInfo = res.data.data.data;
            this.$store.commit("SET_TOKLEN", jwt);
            this.$store.commit("SET_USERINFO", userInfo);
            this.$router.push("/home");
          }else{
            return false;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleRegister() {
      logsApi.register(this.loginForm)
      .then(({data:res})=>{
        console.log(res);
        if(res.code ===200){
          this.$message.success("注册成功，请登录")
          this.status = true;
        }
      })
      .catch(err=>{
        console.log(err);
      })
    },
    clickLogin() {
      this.status = true;
      this.loginForm = {};
    },
    clickRegister() {
      this.status = false;
      this.loginForm = {};
    },
    goHome() {
      this.$router.push("/home");
    },
  },
};
</script>

<style lang="scss" scoped>
.logs-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/img/lobg.png");
  .logs-box {
    box-sizing: border-box;
    border-radius: 5px;
    padding: 30px;
    width: 400px;
    background: #fff;
    box-shadow: 0 0 20px #ccc;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
.el-input,
.el-button {
  width: 300px;
}
.login-register {
  width: 300px;
  display: flex;
  justify-content: space-around;
  font-size: 14px;
  cursor: pointer;
}
.active {
  color: #409eff;
}
.title {
  width: 100%;
  height: 50px;
  text-align: center;
  cursor: pointer;
  color: #409eff;
}
</style>
